/**
 * 
 */
package ch.baeni.jee.model;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import ch.baeni.jee.util.SLL_Globals;
import ch.baeni.jee.util.SLL_Globals.LoginStatus;
import ch.baeni.jee.util.SLL_Globals.UserRole;

/**
 * @author Benjamin
 *
 */
@SuppressWarnings("serial")
@Entity
@Table(name="Users")
public class User implements Serializable {
	@Id @GeneratedValue
	private Long id;

	@Column(name="username", unique=true)
	private String username;

	@Column(name="password")
	private String password;

	@Column(name="active")
	private int active;

	@Column(name="role")
	private String role;

	@Column(name="firstname")
	private String firstname;

	@Column(name="surname")
	private String surname;

	@Column(name="address")
	private String address;

	@Column(name="zipcode")
	private String zipCode;

	@Column(name="location")
	private String location;

	@Column(name="email")
	private String email;

	@Column(name="phone")
	private String phone;

	@Temporal(TemporalType.DATE)
	@Column(name="dateofbirth")
	private Date dateOfBirth;

	@Column(name="gender")
	private String gender;

	@Transient
	private LoginStatus loginStatus;

	/**
	 * Default Konstruktor.
	 */
	public User() {
		super();
	}

	/**
	 * Konstruktor der Klasse User.
	 * Per default ist <code>active=0</code>.
	 *
	 * @param username
	 * @param password
	 * @param firstname
	 * @param surname
	 * @param address
	 * @param zipCode
	 * @param location
	 * @param email
	 * @param phone
	 * @param dateOfBirth
	 * @param gender
	 */
	public User(String username, String password, String firstname,	String surname, String address, String zipCode, String location,
			String email, String phone, Date dateOfBirth, String gender) {
		super();
		this.username = username;
		this.password = SLL_Globals.getHashCode(password);
		this.active = 0;
		this.role = UserRole.user.name();
		this.firstname = firstname;
		this.surname = surname;
		this.address = address;
		this.zipCode = zipCode;
		this.location = location;
		this.email = email;
		this.phone = phone;
		this.dateOfBirth = dateOfBirth;
		this.gender = gender;
	}

	public String getName() {
		return this.firstname + " " + this.surname;
	}

	public Long getId() {
		return id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		if (this.password == null || this.password.compareTo(password) != 0){
			this.password = SLL_Globals.getHashCode(password);
		}
		else{
			this.password = password;
		}
	}

	public boolean isActive() {
		return (1==this.active);
	}

	public void setActive(int active) {
		this.active = active;
	}


	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}


	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public LoginStatus getLoginStatus() {
		return loginStatus;
	}

	public void setLoginStatus(LoginStatus loginStatus) {
		this.loginStatus = loginStatus;
	}

	public boolean isAdmin() {
		return (UserRole.admin.name().equals(this.role));
	}

	@Override
	public String toString() {
		StringBuffer strBuff = new StringBuffer();
		strBuff.append(this.surname);
		strBuff.append(" "+this.firstname);
		strBuff.append(" (").append(this.id).append(")");
		return strBuff.toString();
	}
}
